package cc.ufinity.networkdiagnose.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import cc.ufinity.networkdiagnose.manager.WebAuthManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;

public class AbstractActivity extends Activity {
	
	 private PowerManager pm;
	 private WakeLock mWakeLock;
	 private Handler mHandler;
	 private ProgressDialog messageDialog; 
	    
	@Override
	protected void onPause() {
		try {
			mWakeLock.release();
		} catch(Exception e) {}
		
		super.onPause();
	}
	@Override
	protected void onResume() {
		try {
			mWakeLock.acquire();
		} catch(Exception e) {}
		
		super.onResume();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		try {
			pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
	        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
	        mHandler = new Handler() {

				@Override
				public void handleMessage(Message msg) {
					switch(msg.what) {
					case 1:
						dismissExitDialog();
						exit();
						break;
					}
				}
				 
			 };
	        /*// in onResume() call
	        mWakeLock.acquire();

	        // in onPause() call
	        mWakeLock.release();*/
	        
	        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		} catch(Exception e) {}
	}

	protected void exit() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	private void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");
//		builder.setIcon(R.drawable.what);
		builder.setMessage("确定退出系统吗？");
		builder.setCancelable(false);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				/*WlanManager wlanManager = new WlanManager(AbstractActivity.this);
				if(wlanManager != null && wlanManager.isWifiEnabled() && wlanManager.getCurrentScanResult() != null
						&& wlanManager.getCurrentScanResult().SSID.equals(WlanManager.SSID_CMCC)) {
					webLogout();
				}*/
				webLogout(mHandler);
				dialog.dismiss();
				showExitDialog();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.show();
	}
	/*
	// *添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
	//	menu.add(0, 0, 0, "").setIcon(R.drawable.xml_main_exit);
		menu.add(0, 0, 0, "退    出");

		return super.onCreateOptionsMenu(menu);
	}

	// *管理菜单项
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == 0) {
			showDialog();
		} 
		return super.onOptionsItemSelected(item);
	}*/
	protected void showExitDialog() {
    	messageDialog = new ProgressDialog(this);
        messageDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        messageDialog.setTitle("请稍等");
//        messageDialog.setIcon(R.drawable.smile);
        messageDialog.setMessage("正在退出系统...");
        messageDialog.setCancelable(false);
        messageDialog.setOnKeyListener(new android.content.DialogInterface.OnKeyListener(){
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (KeyEvent.KEYCODE_BACK == keyCode) {
					messageDialog.dismiss();
				}
				return false;
			}
        });
        messageDialog.show();
    }

    protected void dismissExitDialog() {
    	try{
	        messageDialog.dismiss();
    	}catch(Exception e){
    		Log.e("对话框", e.getMessage());
    	}
    }
	public void webLogout(final Handler handler){
		final WebAuthManager webAuthManager = new WebAuthManager(this);
		final WlanManager wlanManager = new WlanManager(AbstractActivity.this);
		if(wlanManager != null) {
			boolean b = wlanManager.isWifiEnabled();
			if(wlanManager.getCurrentScanResult() != null) {
				String s1 = wlanManager.getCurrentScanResult().SSID;
				System.out.println("------ " + s1);
			}
		}
		new Thread() {
			public void run() {
				if((wlanManager != null && wlanManager.isWifiEnabled() && wlanManager.getCurrentScanResult() != null
						&& wlanManager.getCurrentScanResult().SSID.equals(WlanManager.SSID_CMCC))){
					if(webAuthManager.logout2()) {
						System.out.println("------ 退出下线 成功");
					} else {
						System.out.println("------ 退出下线 失败");
					}
				}
				wlanManager.setMobileDataEnabled(false);
				System.out.println("------ 退出系统 关闭数据网络");
				handler.sendEmptyMessage(1);
			}
		}.start();
		
	}
}
